package com.sto.stoapp.activity.administrator;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.listener.StoOnClickListener;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.StoHhmEditText;
import com.sto.stoapp.widget.StoHomeActivity;

public class AdminSiteCodeActivity extends StoHomeActivity {

	// 网点编号长度为6
	private static final int SITE_CODE_LENGTH = 6;

	private StoHhmEditText siteCodeEdit;

	private Button saveBtn;

	private boolean isBackKey = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.admin_site_code);
		this.setTitleBottom(R.string.site_code_number,
				HomeBroadAction.ADMINISTRATOR_BROAD);
		initWidget();
		initListener();
	}

	private void initListener() {
		saveBtn.setOnClickListener(new StoOnClickListener(this) {

			@Override
			public void mHonClick(View view) {
				doSave();
			}
		});
	}

	private void doSave(){

		String siteCode = siteCodeEdit.getText().toString().trim();
		if (siteCode.length() != SITE_CODE_LENGTH) {
			Helper.showToast(AdminSiteCodeActivity.this,
					getText(R.string.site_code_save_failure).toString());
		} else {
			Helper.clearSiteData(AdminSiteCodeActivity.this);
			StoFilePreference.getInstance(AdminSiteCodeActivity.this)
					.setSiteCode(siteCode);

			Helper.showToast(AdminSiteCodeActivity.this,
					getText(R.string.save_success).toString());
			finish();
		}
	
	}
	private void initWidget() {
		String siteCode = StoFilePreference.getInstance(this).getSiteCode();
		siteCodeEdit = (StoHhmEditText) findViewById(R.id.site_code_edit);
		siteCodeEdit.setText(siteCode);
		siteCodeEdit.setSelection(siteCode.length());
		//closeBoard(siteCodeEdit);
		saveBtn = (Button) findViewById(R.id.save);

//		siteCodeEdit.setEditDelete(new EditClickListener() {
//
//			@Override
//			public void onEditDelete() {
//				onBackPressed();
//			}
//
//			@Override
//			public void onEditClickListener(View v) {
//				doSave();
//			}
//		});
	}



}
